<?php
/**
 * Created by JetBrains PhpStorm.
 * File: NodeAction.class.php
 * User: Xnfy520@gmail.com
 * Date: 12-3-17
 * Time: 下午7:56
 */

	class NodeAction extends CommonAction{

		//节点列表
		function index(){

			$node = D('Node');
			import("ORG.Util.page");

			if(isset($_POST['search'])){
				if(empty($_POST['key'])){
					$this->error('不能为空');
				}else{
					$datas = array();
					switch($_POST['search']){
						case 1 :
							$datas['id'] = $_POST['key'];
							break;
						case 2:
							$datas['name'] = array("like","%{$_POST['key']}%");
							break;
					}
					$datas['level'] = 1;
					$datas['pid'] = 0;
					$list = $node->where($datas)->order('id desc')->select();
					foreach($list as $key=>$value){
						$num = $node->where('pid='.$value['id'])->count();
						$list[$key]['count'] = $num;
					}
					$count = $node->where($datas)->count('id');
					if($count<1){
						//	$this->assign('isshow', 'no');
						$this->error('没有该节点数据');
					}elseif($count<C('PAGENUM')){
						$pagedata = array(1);
					}else{
						$pagedata = array(0,1,2,3,4,5,7,8);
					}
					$page = new page($count, C('PAGENUM'));

					$show = $page->fpage($pagedata);
					$this->assign('isshow', 'yes');
					$this->assign('nobody', 'yes');
				}
			}else{
				$datas['level'] = 1;
				$datas['pid'] = 0;
				$count = $node->where($datas)->count();
				if($count<C('PAGENUM')){
					$pagedata = array(1);
				}else{
					$pagedata = array(0,1,2,3,4,5,7,8);
				}
				$page = new page($count, C('PAGENUM'));
				$show = $page->fpage($pagedata);
				$list = $node->where($datas)->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->select();
				foreach($list as $key=>$value){
					$num = $node->where('pid='.$value['id'])->count();
					$list[$key]['count'] = $num;
				}
		//		dump($num);
		//		dump($list);
				if($count==0){
					$this->assign('isshow', 'no');
					$this->assign('nobody', 'no');
				}else{
					$this->assign('isshow', 'yes');
					$this->assign('nobody', 'yes');
				}
			}

			$this->assign('show', $show);
			$this->assign('list', $list);
			$this->display();
		}

		function modelindex(){

			$node = D('Node');
			import("ORG.Util.page");

			if(isset($_POST['search'])){
				if(empty($_POST['key'])){
					$this->error('不能为空');
				}else{
					$datas = array();
					switch($_POST['search']){
						case 1 :
							$datas['id'] = $_POST['key'];
							break;
						case 2:
							$datas['name'] = array("like","%{$_POST['key']}%");
							break;
					}
					if(isset($_GET['pid']) && !empty($_GET['pid'])){
						$datas['pid'] = $_GET['pid'];
						$datas['level'] = 2;
						$list = $node->where($datas)->order('id desc')->select();
						foreach($list as $key=>$value){
							$num = $node->where('pid='.$value['id'])->count();
							$list[$key]['count'] = $num;
						}
						$count = $node->where($datas)->count('id');
						if($count<1){
							//	$this->assign('isshow', 'no');
							$this->error('没有该节点数据');
						}elseif($count<C('PAGENUM')){
							$pagedata = array(1);
						}else{
							$pagedata = array(0,1,2,3,4,5,7,8);
						}
						$page = new page($count, C('PAGENUM'));

						$show = $page->fpage($pagedata);
						$this->assign('isshow', 'yes');
						$this->assign('nobody', 'yes');
					}else{
						$this->error('异常操作');
					}
				}

			}else{
				$datas['pid'] = $_GET['pid'];
				$datas['level'] = 2;
				if(isset($_GET['pid']) && !empty($_GET['pid'])){
					$count = $node->where($datas)->count();
					if($count<C('PAGENUM')){
						$pagedata = array(1);
					}else{
						$pagedata = array(0,1,2,3,4,5,7,8);
					}
					$page = new page($count, C('PAGENUM'));
					$show = $page->fpage($pagedata);

					$list = $node->where($datas)->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->select();
					foreach($list as $key=>$value){
						$num = $node->where('pid='.$value['id'])->count();
						$list[$key]['count'] = $num;
					}
					if($count==0){
						$this->assign('isshow', 'no');
						$this->assign('nobody', 'no');
					}else{
						$this->assign('isshow', 'yes');
						$this->assign('nobody', 'yes');
					}
				}else{
					$this->error('异常操作');
				}

			}

			$this->assign('show', $show);
			$this->assign('list', $list);
			$this->display();
		}

		function actionindex(){

			$node = D('Node');
			import("ORG.Util.page");

			if(isset($_POST['search'])){
				if(empty($_POST['key'])){
					$this->error('不能为空');
				}else{
					$datas = array();
					switch($_POST['search']){
						case 1 :
							$datas['id'] = $_POST['key'];
							break;
						case 2:
							$datas['name'] = array("like","%{$_POST['key']}%");
							break;
					}
					$datas['pid'] = $_GET['pid'];
					$datas['level'] = 3;

					$list = $node->where($datas)->order('id desc')->select();

					$count = $node->where($datas)->count('id');
					if($count<1){
					//	dump($datas);
					//	dump($list);
						//	$this->assign('isshow', 'no');
						$this->error('没有该节点数据');
					}elseif($count<C('PAGENUM')){
						$pagedata = array(1);
					}else{
						$pagedata = array(0,1,2,3,4,5,7,8);
					}
					$page = new page($count, C('PAGENUM'));

					$show = $page->fpage($pagedata);
					$this->assign('isshow', 'yes');
					$this->assign('nobody', 'yes');

				}

			}else{
				$datas['pid'] = $_GET['pid'];
				$datas['level'] = 3;
				$count = $node->where($datas)->count();
				if($count<C('PAGENUM')){
					$pagedata = array(1);
				}else{
					$pagedata = array(0,1,2,3,4,5,7,8);
				}
				$page = new page($count, C('PAGENUM'));
				$show = $page->fpage($pagedata);
				$list = $node->where($datas)->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->select();

				if($count==0){
					$this->assign('isshow', 'no');
					$this->assign('nobody', 'no');
				}else{
					$this->assign('isshow', 'yes');
					$this->assign('nobody', 'yes');
				}
			}

			$this->assign('show', $show);
			$this->assign('list', $list);
			$this->display();
		}

		//指向添加项目节点界面
		function addapp(){
			$node = M('Node');
			$app = array('Home', 'Admin');
			$data = $node->where('level=1')->getField('id, name');
			foreach($data as $key){
				for($i=0; $i<=count($app); $i++){
					if($key==$app[$i]){
						unset($app[$i]);
					}
				}
			}
		//	dump($app);
		//	dump($data);
			if(empty($app)){
				$this->assign("nobody", "no");
			}else{
				$this->assign('app', $app);
			}
			$this->display();
		}

		//指向添加模块节点界面
		function addmodel(){
			if(isset($_GET['pid']) && !empty($_GET['pid'])){
				$model = new Model();
				$list = $model->query("show tables");
				$tables = array();
				$node = M('Node');
				foreach($list as $key=>$value){
					foreach($value as $k){
						$k = str_replace(' ', '', ucwords(str_replace('_', ' ', substr($k, strlen(C('DB_PREFIX'))))));
						if($k=='RoleUser'){
							continue;
						}
						$tables[] = $k;
					}
				}
				$tables[] = 'Index';
//dump($tables);
				$count = count($tables);
	//			$tables = array('Access', 'Article', 'Category', 'Comonent', 'Menu', 'MenuItem', 'Modules', 'Role', 'RoleUser', 'Section', 'User');
				$data = $node->where('level=2 AND pid='.$_GET['pid'])->getField('id, name');
				foreach($data as $key){
						for($i=0; $i<=$count; $i++){
							if($key==$tables[$i]){
								unset($tables[$i]);
								$count+=$i;
							}
						}
				}
			//	dump($tables);
			//	dump($data);

				if(empty($tables)){
					$this->assign("nobody", "no");
				}else{
					$this->assign('tables', $tables);
				}

				$this->display();
			}else{
				$this->error('异常操作');
			}

		}

		//指向添加操作节点界面
		function addaction(){
			if(isset($_GET['pid']) && !empty($_GET['pid'])){
				$node = M('Node');
				$list = $node->where('id='.$_GET['pid'])->find();
			//	dump($list);
				if($list['name']=='Index'){
					$action = array('index', 'systeminfo', 'systemset');
				}elseif($list['name']=='Role'){
					$action = array('index', 'modelindex', 'actionindex');
				}else{
					$action = array('index', 'add','del', 'edit');
				}
				$data = $node->where('level=3 AND pid='.$_GET['pid'])->getField('id, name');
				$count = count($action);
				foreach($data as $key){
					for($i=0; $i<=$count; $i++){
						if($key==$action[$i]){
							unset($action[$i]);
							$count+=$i;
						}
					}
				}
				if(empty($action)){
					$this->assign('nobody', 'no');
				}else{
					$this->assign('action', $action);
				}
				$this->display();
			}else{
				$this->error('异常操作');
			}

		}

		//添加菜单
		function insert(){
			$node = D('Node');
			if($node->create()){
				if($node->add()){
					switch($_POST['add']){
						case 'model' :
							$this->success('添加节点成功', __APP__.'/Node/modelindex/pid/'.$_POST['pid']);
							break;
						case 'action' :
							$this->success('添加节点成功', __APP__.'/Node/actionindex/pid/'.$_POST['pid']);
							break;
						default :
							$this->success('添加节点成功', __APP__.'/Node/index');
							break;
					}
				}else{
					$this->error('添加节点失败');
				}
			}else{
				$this->error($node->getError());
			}


		}

		//删除节点
		function del(){
			if(!empty($_POST['del'])){
				$node = D('Node');
				foreach($_POST['del'] as $key=>$value){
					$list = $node->where('pid='.$value)->select();
				//	dump($list);
					if(empty($list)){
						$num = $node->delete($value);
						if($num>0){
							$this->success('删除节点成功');
						}else{
							$this->error('删除节点失败');
						}
					}else{
						$this->error('该节点下还有子节点,不能删除');
					}

				}
			}else{
				$this->error('请选择节点');
			}
		}

		//指向修改节点界面
		function edit(){
			if($_GET['id'] && !empty($_GET['id'])){
				$node = M('Node');
				$list = $node->find($_GET['id']);

				$this->assign('list', $list);
				$this->display();
			}else{
				$this->error('异常操作');
			}
		}

		//修改分组
		function update(){
			if(isset($_POST['id']) && !empty($_POST['id'])){
				$node = D('Node');
				$pid = $node->where('id='.$_POST['id'])->getField('pid');
				if($list = $node->create()){
					if($node->save()){
						switch($_POST['edit']){
							case 'model' :
								$this->success('修改节点成功', __APP__.'/Node/modelindex/pid/'.$pid);
								break;
							case 'action' :
								$this->success('修改节点成功', __APP__.'/Node/actionindex/pid/'.$pid);
								break;
							default :
								$this->success('修改节点成功', __APP__.'/Node/index');
								break;
						}
					}else{
						$this->error('修改节点失败');
					}
				}else{
					$this->error($node->getError());
				}
			}else{
				$this->error('异常操作');
			}
		}

	}